// package com.hadevg.chatingboomer

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

/**
 * @author HuAng Software Development Group
 * @since 2022.12.11
 * @version 1.0.0.1
 * {@code @TODO:} 2022/12/11 添加一个进度条
 */

// This code was written by HuAng Software Development Group

public class ChatBoomer {

    // 骂狗反编译
    public static void SB_CRACK_DOG(){
        // SB CRACK DOG !!!
        System.out.println("WHAT ARE YOU FUCKING DOING ???");
        System.out.println("仅HuAng Software Development Group内部人员" +
                "可对本程序进行逆向调试");
        System.out.println("逆向狗");
        System.out.println("破解狗");
        System.out.println("反编译狗");
        System.out.println("离我的程序远点！！！");
        System.out.println("滚！！！");
    }

    // 公共变量 常量
    static String name = "聊天轰炸机";                                    // 程序的名称
    static String text_message;                                         // 用于获取输入框"发送内容"
    static String text_times;                                           // 用于获取输入框"发送次数"
    static String text_interval;                                        // 用于获取输入框"发送间隔时间"
    static final String COMMAND_SENT = "sent";                          // 用于监听状态

    // GUI窗口公共类
    public static JFrame frame;                                         // GUI主窗口
    public static JPanel panel;                                         // GUI面板
    public static JLabel made_by,tip_message,tip_times,tip_interval;    // GUI提示控件
    public static JButton button_sent;                                  // GUI按钮控件
    public static JTextField input_message,input_times,input_interval;  // GUI文本输入框控件
    public static JProgressBar progressBar;                             // GUI进度条控件

    // GUI界面设置
    public static int GUIwindow(){
        // 创建窗口控件
        frame = new JFrame(name);
        panel = new JPanel();
        made_by = new JLabel("Made BY HuAng Software Development Group");
//        using = new JLabel("使用造成的一切后果均由使用者承担,与作者无关!!!!!!");
//        using_to_do = new JLabel("事先声明，因使用本软件造成的一切后果与作者无关，如做不到请自行删除本软件.");
        tip_message = new JLabel("轰炸内容：");
        tip_times = new JLabel("轰炸次数：");
        tip_interval = new JLabel("轰炸间隔时间(ms):");
        button_sent = new JButton("开始轰炸");
        input_message = new JTextField(1);
        input_times = new JTextField(1);
        input_interval = new JTextField(1);
        progressBar = new JProgressBar(0,100);
        // 添加控件进panel
        panel.add(made_by);
        panel.add(tip_message);
        panel.add(tip_times);
        panel.add(tip_interval);
        panel.add(button_sent);
        panel.add(input_message);
        panel.add(input_times);
        panel.add(input_interval);
//        panel.add(progressBar);
        // 将面板添加到主窗口
        frame.setContentPane(panel);
        // 设置主窗口属性
        frame.setSize(350, 260);        // 设置大小
        frame.setResizable(false);      // 禁止调整大小 禁止最大化
        frame.setLocationRelativeTo((Component)null);       // 设置初始位置于屏幕正中央
        frame.setDefaultCloseOperation(3);      // 设置点击关闭按钮的操作(关闭软件)
        Image icon = Toolkit.getDefaultToolkit().getImage("");      // 设置图标
        frame.setIconImage(icon);
        frame.setVisible(true);     // 将内存中的GUI数据显示在屏幕上
        // 设置控件属性 位置 大小
        made_by.setLocation(30, 210);
        tip_message.setLocation(25, 25);
        input_message.setSize(200, 30);
        input_message.setLocation(22, 45);
        tip_interval.setLocation(22, 80);
        input_interval.setSize(100, 30);
        input_interval.setLocation(22, 100);
        tip_times.setLocation(25, 135);
        input_times.setSize(50, 30);
        input_times.setLocation(22, 155);
        progressBar.setSize(200, 6);
        button_sent.setLocation(110, 157);
        progressBar.setLocation(70, 195);
        button_sent.setActionCommand(COMMAND_SENT);
        return 0;
    }

    // 定义将字符串存入剪贴板函数
    public static void ToClickBoard(String message){
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(new StringSelection(message),null);
    }

    // 定义延迟函数
    public static int sleep(int times_ms){
        // sleep(time_ms)
        try {
            Thread.sleep(times_ms);
        } catch (Exception ignored) {
            return 1;
        }
        return 0;
    }

    // 检查用户输入是否符合要求
    // 符合返回0 不符合返回1
    public static int input_check(String message, String times_w, String interval_w){
        times_w = times_w.replaceAll(" ","");
        interval_w = interval_w.replaceAll(" ","");
        int message_len,times,interval;
        message_len = message.length();
        try {
            // 时间和次数中是否包含异常字符
            times = Integer.parseInt(times_w);
            interval = Integer.parseInt(interval_w);
        } catch (NumberFormatException e) {
            return 1;
        }
        times = Integer.parseInt(times_w);
        interval = Integer.parseInt(interval_w);
        // 要被发送的消息字数小于100 要连续发送的次数小于201 要发送的间隔时间不小于11m
        if (message_len > 100 || times > 200 || interval < 10) {
            return 1;
        } else {
            return 0;
        }
    }

    // 发送函数，模拟command+v，回车  传入重复次数和间隔时间
    public static int sent_vk(int times, int interval) {
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
        }
        if (robot == null) {
            return 1;
        }
        robot.mousePress(0);
        robot.delay(1);
        robot.mouseRelease(0);
        for (int i = 0; i != times; i++) {
            robot.keyPress(KeyEvent.VK_META);
            robot.keyPress(KeyEvent.VK_V);
            robot.delay(1);
            robot.keyRelease(KeyEvent.VK_META);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.delay(1);
            robot.keyRelease(KeyEvent.VK_ENTER);
            sleep(interval);
        }
        return 0;
    }

    public static void main(String[] args) {
        new ChatBoomer();
        int UIresult;
        UIresult = ChatBoomer.GUIwindow();
        if (UIresult != 0){
            System.exit(1);
        }
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String command = e.getActionCommand();
                if (ChatBoomer.COMMAND_SENT.equals(command)) {
                    ChatBoomer.button_sent.setEnabled(false);
                    ChatBoomer.text_message = ChatBoomer.input_message.getText();
                    ChatBoomer.text_interval = ChatBoomer.input_interval.getText();
                    ChatBoomer.text_times = ChatBoomer.input_times.getText();
                    int input_check_result = input_check(text_message, text_times, text_interval);
                    if (input_check_result != 0) {
                        // 输入有误，弹出提示框
                        JOptionPane.showMessageDialog(null,"您的输入有误","输入值错误",
                                0);
                        ChatBoomer.button_sent.setEnabled(true);
                    } else {
//                        System.out.println("111");
                        String message;
                        int times, interval;
                        times = Integer.parseInt(text_times);
                        interval = Integer.parseInt(text_interval);
                        message = text_message;
                        // 准备发送
                        ChatBoomer.ToClickBoard(message);
                        for (int i = 0; i < ; i++) {
                            ChatBoomer.
                        }
                        ChatBoomer.sleep(5000);
                        int robots_return;
                        robots_return = ChatBoomer.sent_vk(times,interval);
                        if (robots_return != 0){
                            System.exit(1);
                        }
                        ChatBoomer.button_sent.setEnabled(true);
                        ToClickBoard("");
                    }
                }
            }
        };
        button_sent.addActionListener(listener);
    }
}